package fr.lille1.wordcount.controller;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.lille1.wordcount.service.IWordCountService;
import fr.lille1.wordcount.service.impl.WordCountServiceImpl;

/**
 * Controller pour worcount service
 * @author RAKOTOBE Sitraka Eric
 */
public class WordCountController extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	private IWordCountService wordCountService;
	

	@Override
	public void init() throws ServletException {
		super.init();
		wordCountService = new WordCountServiceImpl();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse respone) throws ServletException, IOException {
		
		if (request.getParameter("countAllWordInTextSubmit") != null){
			String textcountAllWordInText = request.getParameter("textcountAllWordInText");
			request.setAttribute("countAllWordInTextResult", wordCountService.countAllWordInText(textcountAllWordInText));
		}
		
		if (request.getParameter("countOneWordInTextSubmit") != null){
			String wordCountOneWordInText = request.getParameter("wordCountOneWordInText");
			String textCountOneWordInText = request.getParameter("textCountOneWordInText");
			request.setAttribute("countOneWordInTextResult", wordCountService.countOneWordInText(wordCountOneWordInText, textCountOneWordInText));
		}
		
		
		if (request.getParameter("isWordInTextSubmit") != null){
			String wordIsWordInText = request.getParameter("wordIsWordInText");
			String textIsWordInText = request.getParameter("textIsWordInText");
			request.setAttribute("isWordInTextResult", wordCountService.isWordInText(wordIsWordInText, textIsWordInText));
		}		
		
		

		if (request.getParameter("getAllFrequentWordsInTextSubmit") != null){
			String textGetAllFrequentWordsInText = request.getParameter("textGetAllFrequentWordsInText");
			String wordsGetAllFrequentWordsInText = request.getParameter("wordsGetAllFrequentWordsInText");
			Integer nbWordsGetAllFrequentWordsInText = Integer.valueOf(request.getParameter("nbWordsGetAllFrequentWordsInText"));
			request.setAttribute("getAllFrequentWordsInTextResult", wordCountService.getAllFrequentWordsInText(textGetAllFrequentWordsInText, wordsGetAllFrequentWordsInText, nbWordsGetAllFrequentWordsInText));
		}	
		
		
		request.getRequestDispatcher("index.jsp").forward(request, respone);
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse respone) throws ServletException, IOException {
		this.doPost(request, respone);
	}

	
			
	
}
